package app

import (
	"os"
	"path/filepath"
	"v3workshop/pkg/config"
	"v3workshop/pkg/parcel"
)

type Manager struct {
	Config  *config.Manager
	Parcels []*parcel.Parcel
}

func NewManager() *Manager {
	cm := config.NewManager()
	return &Manager{
		Config: cm,
	}
}

func (m *Manager) LoadConfig() error {
	err := m.Config.Load()
	if err != nil {
		return err
	}
	return nil
}

func (m *Manager) LoadParcels() error {
	dirEntrise, err := os.ReadDir(m.Config.SourcePath)
	if err != nil {
		return err
	}
	for _, entry := range dirEntrise {
		if entry.IsDir() {
			p := parcel.NewParcel(filepath.Join(m.Config.SourcePath, entry.Name()))
			err = p.LoadMetadata()
			if err != nil {
				return err
			}
			m.Parcels = append(m.Parcels, p)
		}
	}
	return nil
}
